package pl.edu.agh.iosr.gamblingzone.dao.ejb;

import java.util.Collection;
import java.util.LinkedList;

import pl.edu.agh.iosr.gamblingzone.dao.MoneyVoucherDAO;
import pl.edu.agh.iosr.gamblingzone.model.MoneyVoucher;
import pl.edu.agh.iosr.gamblingzone.model.User;

/**
 * The Class MoneyVoucherDAOBean.
 */
public class MoneyVoucherDAOBean extends GenericDAOBean<MoneyVoucher, Long> implements MoneyVoucherDAO {

	/* (non-Javadoc)
	 * @see pl.edu.agh.iosr.gamblingzone.dao.MoneyVoucherDAO#findAll(pl.edu.agh.iosr.gamblingzone.model.User, int)
	 */
	public Collection<MoneyVoucher> findAll(User user, int page) {
		
		return findAll(user, "owner", page);
	}

	/* (non-Javadoc)
	 * @see pl.edu.agh.iosr.gamblingzone.dao.MoneyVoucherDAO#getEntitiesCount(pl.edu.agh.iosr.gamblingzone.model.User)
	 */
	public int getEntitiesCount(User u) {
		return getEntitiesCount(u, "owner");
	}

	/* (non-Javadoc)
	 * @see pl.edu.agh.iosr.gamblingzone.dao.MoneyVoucherDAO#findAllActive(pl.edu.agh.iosr.gamblingzone.model.User, int)
	 */
	public Collection<MoneyVoucher> findAllActive(User user, int page) {
		// TODO temporary workaround - query needed

		int oldPageSize = getPageSize();
		setPageSize(1000);
		Collection<MoneyVoucher> res = findAll(user, 0);
		setPageSize(oldPageSize);
		
		Collection<MoneyVoucher> retCol = new LinkedList<MoneyVoucher>();
		
		for (MoneyVoucher mv: res) {
			if (mv.isActive()) {
				retCol.add(mv);
			}
		}
		
		return retCol;
	}

	/* (non-Javadoc)
	 * @see pl.edu.agh.iosr.gamblingzone.dao.MoneyVoucherDAO#getActiveEntitesCount(pl.edu.agh.iosr.gamblingzone.model.User)
	 */
	public int getActiveEntitesCount(User u) {
		// TODO temporary workaround - query needed
		return findAllActive(u, 0).size();
	}
}
